package com.naiterui.ehp.bs.emr.repository.mongo;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Optional;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.naiterui.common.repository.core.BaseMongoRepository;
import com.naiterui.ehp.bs.emr.entity.mongo.MedicalRecord;

/**
 * @author wangzehao
 * @date 2018/10/16 2:01 PM
 * @since 1.0.0
 */
@Repository
public interface MedicalRecordRepository extends BaseMongoRepository<MedicalRecord, String> {

    /**
     * 通过医生ID，推荐信息查询病历信息
     *
     * @param doctorId    医生ID
     * @param recommendId 推荐ID
     *
     * @return
     */
    @Query("{$and:[{'doctorId' : ?#{#doctorId} }," +
            "{$or:[{ 'recordRecom.recommendId' : ?#{#recommendId} },{ 'recordDoctor.recommend.recommendId' : ?#{#recommendId} }]}]}")
    MedicalRecord findByDoctorIdAndRecommendId(@Param("doctorId") Long doctorId, @Param("recommendId") Long recommendId);

    /**
     * 根据推荐id查询医生填写病历
     *
     * @param recommendId
     *
     * @return
     */
    @Query("{'recordDoctor.recommend.recommendId' : ?#{#recommendId} }")
    MedicalRecord getMRByRecomId(@Param("recommendId") Long recommendId);

    /**
     * 根据患者填写量表编号查询病历
     *
     * @return
     */
    @Query("{'recordScale.extBizId' : ?#{#extBizId} }")
    MedicalRecord getMRByPatientScaleId(@Param("extBizId") String extBizId);

    @Query(value = "{ $and: [{" +
            "'doctorId' : ?#{#doctorId} , " +
            "'patientId' : ?#{#patientId} , " +
            "'caseType' : { $in : ?#{#caseType}} ," +
            "'createAt' : { $gte : ?#{#startTime}, $lte : ?#{#endTime}} " +
            "}] }", exists = true)
    Boolean existsMedicalRecord(@Param("doctorId") Long doctorId,
                                @Param("patientId") Long patientId,
                                @Param("caseType") List<Integer> caseType,
                                @Param("startTime") Date startTime,
                                @Param("endTime") Date endTime);

    @Query(value = "{'doctorId' : ?#{#doctorId} , 'patientId' : ?#{#patientId} , 'caseType' : { $in : ?#{#caseType}} }", exists = true)
    boolean existMedicalRecordByPatient(@Param("doctorId") Long doctorId,
                                        @Param("patientId") Long patientId,
                                        @Param("caseType") List<Integer> caseType);

    @Query("{ 'recordScale.extBizId' : ?#{#extBizId} }")
    Optional<MedicalRecord> findByExtBizId(@Param("extBizId") String extBizId);

    /**
     * 根据患者线上诊室病历id获取医生病历
     *
     * @param recordId
     *
     * @return
     */
    @Query("{ 'recordConsult.recordId' : ?#{#recordId} }")
    MedicalRecord findByPtRecordId(@Param("recordId") String recordId);


    /**
     * <pre>
     *     获取患者病例列表
     * </pre>
     * @param patientId
     * @param pageable
     * @return
     */
    Page<MedicalRecord> findByPatientId(Long patientId, Pageable pageable);


    Page<MedicalRecord> findByPatientIdAndSendAndCaseTypeNot(Long patientId, Boolean send, Integer caseType,
        Pageable pageable);

    /**
     * <pre>
     *     查询已发送的患者病例
     * </pre>
     * @param patientId
     * @param send
     * @param pageable
     * @return
     */
    Page<MedicalRecord> findByPatientIdAndSend(Long patientId,Boolean send, Pageable pageable);


    MedicalRecord findByIdAndCaseTypeAndAvailableNot(String id, Integer caseTypem, Integer available);

    @Query("{ 'sessionInfo.sessionId' : ?#{#sessionId} }")
    List<MedicalRecord> findBySessionId(@Param("sessionId") String sessionId);
	
	/**
     * 根据就诊人id查询
     *
     * @param inquirerId
     * @param send
     * @param pageable
     * @return
     */
    Page<MedicalRecord> findByInquirerIdAndSend(Long inquirerId,Boolean send, Pageable pageable);

    Page<MedicalRecord> findByInquirerIdAndSendAndCaseTypeNot(Long inquirerId,Boolean send, Integer caseType, Pageable pageable);

    List<MedicalRecord> findAllByIdIn(Collection<String> ids);

    @Query("{ 'sessionInfo.sessionId' : { $in : ?#{#sessionIds}} }")
    List<MedicalRecord> findBySessionIds(@Param("sessionIds") List<String> sessionIds);
}
